package canss.webservice.servlets;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import canss.supports.basic;
import canss.webservice.model.EventDescription;
import canss.webservice.service.PortalIntraWebServiceControl;



import canss.webservice.service.PortalIntraWebServiceControlBean;

/**
 * Servlet implementation class for Servlet: svtAjaxEditEventDesc
 * 
 */
public class svtAjaxEditEventDesc extends javax.servlet.http.HttpServlet
		implements javax.servlet.Servlet
{
	private static final long serialVersionUID = 1897337121689022495L;
	PortalIntraWebServiceControl control; // control = (control==null)?new

	// PortalIntraWebServiceControlBean():control;

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#HttpServlet()
	 */
	public svtAjaxEditEventDesc()
	{
		super();
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		doPost(request, response);
	}

	/*
	 * (non-Java-doc)
	 * 
	 * @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest request,
	 *      HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException
	{
		response.setContentType("text/html; charset=UTF-8");
		StringBuffer retstr = new StringBuffer();
		PrintWriter out = response.getWriter();

		try
		{
			String _methodname = request.getParameter("_methodname");
			if (!basic.isEmpty(_methodname))
			{
				if ("add".equalsIgnoreCase(_methodname))
				{
					String _eventid = request.getParameter("_eventid");
					String _level = request.getParameter("_level");
					String _eventname = request.getParameter("_eventname");
					String _oneLineDesc = request.getParameter("_oneLineDesc");

					if (!(basic.isEmpty(_eventid) | basic.isEmpty(_eventid)
							| basic.isEmpty(_eventid) | basic.isEmpty(_eventid)))
					{
						control = (control == null) ? new PortalIntraWebServiceControlBean()
								: control;
						EventDescription eventdesc = new EventDescription();
						eventdesc.setEVENTID(Integer.parseInt(_eventid));
						eventdesc.setLEVEL(Integer.parseInt(_level));
						eventdesc.setEVENTNAME(_eventname);
						eventdesc.setONELINEDESC(_oneLineDesc);
						
						control.addEventDesc(eventdesc);
						
					}
				}else if("remove".equalsIgnoreCase(_methodname))
				{
					String _eventid = request.getParameter("_eventid");
					if (!(basic.isEmpty(_eventid)))
					{
						control = (control == null) ? new PortalIntraWebServiceControlBean()
						: control;
						control.removeEventDesc(Integer.parseInt(_eventid));
					}
				}
			}
		} catch (RuntimeException e)
		{
			// TODO Auto-generated catch block
			e.printStackTrace();
			retstr.append(e.toString() + "<br/>");
		}
		out.write(retstr.toString());

		String _rurl = request.getParameter("_rurl");
		if (!basic.isEmpty(_rurl))
		{
			response.sendRedirect(response.encodeRedirectURL(_rurl));
		}
	}
}